package web.admin.cms;

import dao.cms.UrlAlreadyExistsException;
import domain.cms.AbstractPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.CmsAdminService;

import java.util.logging.Level;
import java.util.logging.Logger;

public final class PageEdit extends Panel {

    @SpringBean
    private CmsAdminService cmsService;
    private static Logger logger = Logger.getLogger(PageEdit.class.getName());

    PageEdit(String id, final IModel<? extends AbstractPage> pageModel, final CmsAdmin parent) {
        super(id);
        add(new Form("edit") {

            {
                add(new CmsAttributeEdit("attributes", pageModel));

                /*  Button deleteButton = new Button("delete") {

                                        @Override
                    public void onSubmit() {
                        try {
                            super.onSubmit();
                            cmsService.removePage(pageModel.getObject());
                            getSession().info(getLocalizer().getString("page.success.deleted", this));
                            parent.replacePanelToList();
                        } catch (Exception e) {
                            logger.log(Level.SEVERE, null, e);
                            error(getLocalizer().getString("page.cannot.delete", this));
                        }

                    }
                };
                add(deleteButton);
                */
                add(new Button("save") {

                    @Override
                    public void onSubmit() {
                        try {
                            cmsService.updateCmsPage(pageModel.getObject());
                            info(getLocalizer().getString("system.ok", this));
                        } catch (UrlAlreadyExistsException e) {
                            logger.log(Level.WARNING, null, e);
                            error(getLocalizer().getString("page.error.url", this));
                        }
                        catch (Exception e) {
                            logger.log(Level.SEVERE, null, e);
                            error(getLocalizer().getString("system.error", this));
                        }

                    }
                });
            }
        });
    }
}
